package com.rockbb.common.sso.session;

import java.io.Serializable;

import org.codehaus.jackson.map.annotate.JsonView;

import com.rockbb.common.utilities.security.StringDigest;
import com.rockbb.common.utilities.text.JacksonViews;

public class Session implements Serializable
{
	private static final long serialVersionUID = -6171213392145137763L;
	private String session_id;
	private String session_token;
	private long session_uuid;
	private long session_start;
	private long session_time;
	private String session_ip;
	private String session_browser;
	private int session_autologin;
	private int session_secure;

	public Session()
	{
		this.session_id = StringDigest.unique_id();
	}

	@JsonView(JacksonViews.Public.class)
	public String getSession_id()
	{
		return session_id;
	}

	public void setSession_id(String s)
	{
		this.session_id = s;
	}

	@JsonView(JacksonViews.Public.class)
	public long getSession_uuid()
	{
		return session_uuid;
	}

	public void setSession_uuid(long session_uuid)
	{
		this.session_uuid = session_uuid;
	}

	@JsonView(JacksonViews.Public.class)
	public long getSession_start()
	{
		return session_start;
	}

	public void setSession_start(long i)
	{
		this.session_start = i;
	}

	@JsonView(JacksonViews.Public.class)
	public long getSession_time()
	{
		return session_time;
	}

	public void setSession_time(long i)
	{
		this.session_time = i;
	}

	@JsonView(JacksonViews.Internal.class)
	public String getSession_ip()
	{
		return session_ip;
	}

	public void setSession_ip(String s)
	{
		this.session_ip = s;
	}

	@JsonView(JacksonViews.Internal.class)
	public int getSession_secure()
	{
		return session_secure;
	}

	public void setSession_secure(int i)
	{
		this.session_secure = i;
	}

	@JsonView(JacksonViews.Internal.class)
	public String getSession_browser()
	{
		return session_browser;
	}

	public void setSession_browser(String s)
	{
		this.session_browser = s;
	}

	@JsonView(JacksonViews.Public.class)
	public int getSession_autologin()
	{
		return session_autologin;
	}

	public void setSession_autologin(int i)
	{
		this.session_autologin = i;
	}

	@JsonView(JacksonViews.Internal.class)
	public String getSession_token()
	{
		return session_token;
	}

	public void setSession_token(String token)
	{
		this.session_token = token;
	}

	@JsonView(JacksonViews.Internal.class)
	public boolean is_expired()
	{
		if (session_uuid == SessionConstants.USER_UID_ANONYMOUS)
		{
			if (System.currentTimeMillis() - SessionConstants.SESSION_EXPIRE_ANONYMOUS * 1000
					- session_time > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			if (session_autologin == SessionConstants.SESSION_AUTOLOGIN_OFF)
			{
				if (System.currentTimeMillis() - SessionConstants.SESSION_EXPIRE * 1000
						- session_time > 0)
				{
					return true;
				}
				else
				{
					return false;
				}
			}
			else
			{
				if (System.currentTimeMillis() - SessionConstants.SESSION_EXPIRE_MAX * 1000
						- session_time > 0)
				{
					return true;
				}
				else
				{
					return false;
				}
			}
		}

	}

	public String toString() {
		return session_id 
		+", "+session_uuid
		+", "+this.session_time
		+", "+this.session_start
		+", "+this.session_ip; 
	}
}
